package com.example.restaurant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.restaurant.common.RestaurantException;
import com.example.restaurant.controller.form.AddDishForm;

import com.example.restaurant.controller.form.AddOrderOperateForm;
import com.example.restaurant.controller.form.ModifyDishNumberByStatusForm;
import com.example.restaurant.dao.DishMapper;
import com.example.restaurant.dao.OrderInfoMapper;
import com.example.restaurant.model.Dish;
import com.example.restaurant.model.DishOperate;
import com.example.restaurant.dao.DishOperateMapper;
import com.example.restaurant.service.DishOperateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.restaurant.service.OrderInfoService;
import com.example.restaurant.service.param.AddOrderOperateParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 加退菜表 服务实现类
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@Service
public class DishOperateServiceImpl extends ServiceImpl<DishOperateMapper, DishOperate> implements DishOperateService {
    @Resource
    private DishOperateMapper dishOperateMapper;
    @Resource
    private OrderInfoService orderInfoService;
    @Override
    @Transactional
    public void AddDish(AddOrderOperateParam param) {
        param.setTime(new Date());
        DishOperate dishOperate = BeanUtil.copyProperties(param, DishOperate.class);
        dishOperate.setDishNumber(param.getDishOperateNumber());
        ModifyDishNumberByStatusForm modifyDishNumberByStatusForm = new ModifyDishNumberByStatusForm();
        modifyDishNumberByStatusForm.setDishId(param.getDishId());
        modifyDishNumberByStatusForm.setOrderNumber(param.getOrderNumber());
        modifyDishNumberByStatusForm.setNumber(param.getDishOperateNumber());
        modifyDishNumberByStatusForm.setStatus(param.getStatus());
        orderInfoService.modifyOrderNumber(modifyDishNumberByStatusForm);
        int insert = dishOperateMapper.insert(dishOperate);
        if (insert<1){
            throw new RestaurantException("添加加退菜表失败");
        }
    }

//        for (AddDishForm addDishForms :form) {
//            DishOperate dishOperate = BeanUtil.copyProperties( addDishForms, DishOperate.class);
//            dishOperate.setTime(new Date());
//            int insert = dishOperateMapper.insert(dishOperate);
//            if (insert<1){
//                throw new RestaurantException("加退菜表添加失败");
//            }
//            ModifyDishNumberForm modifyDishNumberForm = new ModifyDishNumberForm();
//            modifyDishNumberForm.setDishId(dishOperate.getDishId());
//            Dish dish = dishMapper.selectById(dishOperate.getDishId());
//            if (dishOperate.getStatus().equals("REMOVE_DISH")){
//                modifyDishNumberForm.setDishNumber(dish.getStock()+dishOperate.getDishNumber());
//            }else {
//                modifyDishNumberForm.setDishNumber(dish.getStock()-dishOperate.getDishNumber());
//                modifyDishNumberForm.setQuantity(dish.getQuantity()+dishOperate.getDishNumber());
//            }
//            dishMapper.modifyDishNumber(modifyDishNumberForm);
//        }
//
//    }

}
